package com.hnit.provider.service.impl;

import com.hnit.pojo.User;
import com.hnit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

//@Service//将该类的对象创建出来，放到Spring的IOC容器中  bean定义
//将这个类提供的方法（服务）对外发布。将访问的地址 ip，端口，路径注册到注册中心中
@Slf4j
@DubboService
public class UserServiceImpl implements UserService {
    // 用来记录被调用的次数
    private final AtomicInteger sum = new AtomicInteger(0);
    // 模拟数据
    private static final List<User> USERS = Arrays.asList(
            new User(1, "张三","111"),
            new User(2, "李四","222")
    );

    public String sayHello() {
        return "hello dubbo ~";
    }


    public User findUserById(int id) {
        System.out.println("3...");
        //查询User对象
        User user = new User(1,"国王","123456");
/*
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
*/
        return user;
    }

    @Override
    public User getByUserId(Integer userId) {
        // 打印一下被调用情况
        log.info("dubbo-provider被调用【{}】次", sum.incrementAndGet());
        for (User user : USERS) {
            if (user.getId().equals(userId)) {
                return user;
            }
        }
        return null;
    }
}
